package com.sc.nft.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.sc.nft.annotation.UserLogin;
import com.sc.nft.entity.dto.DisplayTypeDTO;
import com.sc.nft.entity.dto.GetWorkRoomOrCityServiceNameDTO;
import com.sc.nft.entity.dto.ServiceCenterAuditAddDTO;
import com.sc.nft.entity.vo.*;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.ServiceCenterAuditService;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * (ServiceCenterAudit)表控制层
 *
 * @author wwc
 * @since 2023-07-19 18:36:13
 */

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("serviceCenterAudit")
@Api(tags = "申请成为社区网点")
public class ServiceCenterAuditController {

    /**
     * 服务对象
     */
    private final ServiceCenterAuditService serviceCenterAuditService;

    private final RedissonClient redissonClient;

    @PostMapping(value = "/applyFor", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "申请", notes = "申请", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result applyFor(@Validated @RequestBody ServiceCenterAuditAddDTO serviceCenterAuditAddDTO) {
        String key = StrUtil.format("applyFor:{}", UserPool.getUser().getId());
        RLock lock = redissonClient.getLock(key);
        boolean b = lock.tryLock();
        if (!b) {
            throw new GlobalRunTimeException("请勿重复提交");
        }
        try{
            serviceCenterAuditService.add(serviceCenterAuditAddDTO);
        }finally {
            lock.unlock();
        }
        return Result.success();
    }

    @PostMapping(value = "/displayType", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "申请状态", notes = "申请状态", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<ServiceCenterAuditDisplayTypeVO> displayType(@Validated @RequestBody DisplayTypeDTO dto) {
        return Result.success(serviceCenterAuditService.displayType(UserPool.getUser(), Convert.toInt(dto.getType())));
    }

    @PostMapping(value = "/withdraw", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "撤回申请", notes = "撤回申请", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result withdraw(@Validated @RequestBody DisplayTypeDTO dto) {
        serviceCenterAuditService.withdraw(UserPool.getUser(),dto.getType());
        return Result.success();
    }

    @PostMapping(value = "/recyclingBin", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "回收箱", notes = "回收箱", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<ServiceCenterAuditDetailsVO> recyclingBin(@Validated @RequestBody DisplayTypeDTO dto) {
        ServiceCenterAuditDetailsVO serviceCenterAuditDetailsVO = serviceCenterAuditService.recyclingBin(UserPool.getUser(),dto.getType());
        return Result.success(serviceCenterAuditDetailsVO);
    }

    @PostMapping(value = "/getWorkRoomOrCityServiceName", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取工作室或城市服务中心名字", notes = "获取工作室或城市服务中心名字", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<GetWorkRoomOrCityServiceNameVO> getWorkRoomOrCityServiceName(@RequestBody GetWorkRoomOrCityServiceNameDTO getWorkRoomOrCityServiceNameDTO) {
        return Result.success(serviceCenterAuditService.getWorkRoomOrCityServiceName(UserPool.getUser(), getWorkRoomOrCityServiceNameDTO.getType()));
    }

    @PostMapping(value = "/performance", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "业绩", notes = "业绩", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<PerformanceVO> performance() {
        return Result.success(serviceCenterAuditService.performance(UserPool.getUser()));
    }


    @PostMapping(value = "/newPerformance", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "新版成为社区网点和服务中心后的业绩", notes = "新版成为社区网点和服务中心后的业绩", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<NewPerformanceVO> newPerformance() {
        return Result.success(serviceCenterAuditService.newPerformance(UserPool.getUser()));
    }




}
